/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2025-01-04
 * Time: 13:08
 */
class Solution3 {
    public String longestPalindrome(String s) {
        int n = s.length();
        if (n == 1) return s;

        int ret = 1;  // 最大回文长度
        String str = s.substring(0, 1);  // 初始化结果字符串
        int left = 0, right = 0;

        for (int cur = 0; cur < n; cur++) {  // 从第一个字符开始
            // 检查以 cur 为中心的奇数长度回文
            left = cur;
            right = cur;
            while (left >= 0 && right < n && s.charAt(left) == s.charAt(right)) {
                left--;
                right++;
            }
            int curLen = right - left - 1;
            if (curLen > ret) {
                ret = curLen;
                str = s.substring(left + 1, right);  // 截取回文子串
            }

            // 检查以 cur 和 cur+1 为中心的偶数长度回文
            left = cur;
            right = cur + 1;
            while (left >= 0 && right < n && s.charAt(left) == s.charAt(right)) {
                left--;
                right++;
            }
            curLen = right - left - 1;
            if (curLen > ret) {
                ret = curLen;
                str = s.substring(left + 1, right);  // 截取回文子串
            }
        }
        return str;
    }
}

public class Test3 {
    public static void main(String[] args) {
        Solution3 solution3 = new Solution3();
        String str = "bb";
        solution3.longestPalindrome(str);
    }
}
